<template>
	<view>
		<q-field label="商品标题" v-model="dataForm.title" :required="true" placeholder="请填写商品标题"></q-field>
		<q-field label="商品介绍" type="textarea" v-model="dataForm.description" :required="true" placeholder="请填写商品介绍">
		</q-field>
		<view class="form-item">
			<view class="label">商品图片<text style="color: red;">*</text></view>
			<q-upload ref="ossUpload" @success="onSubmit"></q-upload>
		</view>
		<q-field label="价格" type="number" v-model="dataForm.price" :required="true" placeholder="请填写商品价格"></q-field>
		<q-field label="联系微信 (选填)" type="text" v-model="dataForm.wechat" :required="true"
			placeholder="请填写联系微信"></q-field>
		<q-field label="联系qq (选填)" type="number" v-model="dataForm.qq" :required="true" placeholder="请填写联系qq"></q-field>
		<!-- 提交按钮 -->
		<view style="padding: 50rpx 0;">
			<q-button shape="circle" @click="onUpload">提交</q-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataForm: {
					topic_id: '',
					title: '',
					description: '',
					price: '',
					img_list: [],
					wechat: '',
					qq: ''
				}
			};
		},
		computed: {
			userInfo() {
				return this.$store.state.userInfo
			},
			currentTopicId() {
				return this.$store.state.currentTopicId;
			}
		},
		onShow() {
			if (!this.userInfo) {
				uni.navigateTo({
					url: "/pages/user/login"
				})
			}
		},
		methods: {
			onSubmit(e) {
				uni.showLoading({
					mask: true,
					title: "提交中"
				})
				let that = this;

				let fileList = [];
				e.forEach(item => {
					if (item.url) {
						fileList.push(item.url)
					}
				})
				this.dataForm.img_list = fileList;
				this.dataForm.topic_id = this.currentTopicId;

				this.$H.post('goods/release', this.dataForm).then(res => {
					if (res.code === 1) {
						uni.reLaunch({
							url: "/pages/index/index"
						})

						this.$H.post('goods/checkGoodsImg', {
							goods_id: res.result.id
						});
					}

					uni.hideLoading()
				});
			},
			onUpload() {
				let that = this;
				if (!this.dataForm.title) {
					this.$u.toast('标题不能为空');
					return;
				}

				if (!this.dataForm.description) {
					this.$u.toast('商品介绍不能为空');
					return;
				}

				if (this.$refs.ossUpload.fileList.length === 0) {
					this.$u.toast('请上传商品图片');
					return;
				}

				if (!this.dataForm.price) {
					this.$u.toast('请填写商品价格');
					return;
				}


				// #ifdef MP-WEIXIN
				wx.requestSubscribeMessage({
					tmplIds: [that.$c.replyTmplId],
					complete() {
						that.$refs.ossUpload.upload();

						uni.showLoading({
							mask: true,
							title: '发布中'
						});
					},
					fail(err) {
						console.log(err, '订阅消息错误')
					}
				})
				// #endif

				// #ifdef H5
				this.$refs.ossUpload.upload();

				uni.showLoading({
					mask: true,
					title: '发布中'
				});
				// #endif
			},
		}
	};
</script>

<style lang="scss" scoped>
	page {
		background-color: #fff;
	}

	.upload-wrap {
		padding: 30rpx;
	}

	.form-item {
		background-color: #fff;
		padding: 30rpx;

		.label {
			margin-bottom: 10rpx;
		}
	}
</style>